/**
 * Created by IAN on 2016/10/14.
 */
$(function () {
    //加载省
    var selProvince = $.Params.create();
    selProvince.ajax(getIP() + "memberHospital/getprovince", function (data) {
        if (data.success) {
            var provinceHtml = "<option value = '0'>请选择省份</option>";
            $.each(data.rows, function (i, val) {
                provinceHtml += "<option value = '" + val.id + "'>" + val.province + "</option>";
            })
            $("#province").html(provinceHtml);
        }
    })
    //加载市
    $("#province").on("change", function () {
        var province = $("#province").find("option:selected").text();
        var cityHtml = "<option value = '0'>请选择城市</option>";
        if (province == "请选择省份") {
            $("#city").html(cityHtml);
        } else {
            var selCity = $.Params.create();
            selCity.addParams("province", province);
            selCity.ajax(getIP() + "memberHospital/getcityByprovince", function (data) {
                if (data.success) {
                    $.each(data.rows, function (i, val) {
                        cityHtml += "<option value = '" + val.province + "'>" + val.city + "</option>";
                    })
                    $("#city").html(cityHtml);
                }
            })
        }

    })

    //加载医院
    /*  $("#city").on("change",function(){
          var city = $("#city").find("option:selected").text();
          var hospitalHtml = "<option value = '0'>请选择医院</option>";
          if(city == "请选择城市"){
              $("#hospitalName").html(hospitalHtml);
          }else{
              var hospitalSel= $.Params.create();
              hospitalSel.addParams("city",city);
              hospitalSel.ajax(getIP()+"memberHospital/gethospitalBycity",function(data){
                  if(data.success){
                      $.each(data.rows,function(i,val){
                          hospitalHtml+="<option value = '"+val.city+"'>"+val.name+"</option>"
                      })
                      $("#hospitalName").html(hospitalHtml);
                  }
              })
          }
      })
      */
    //日历控件
    $('#data_1 #datepicker').datepicker({
        todayBtn: "linked",
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true,
        clearBtn: true
    });



    var sel = $.Params.create();
    sel.addParams("page", 0);
    sel.addParams("rows", rows);
    sel.addParams("type", 0);
    getData();

    //查询
    $("#searchBtn").on("click", function () {
        var tel = $("#tel").val().trim();
        var province = $("#province").find("option:selected").text();
        var city = $("#city").find("option:selected").text();
        var hospitalName = $("#hospitalName").val().trim();
        var hospitalOffices = $("#hospitalOffices").val().trim();
        var startTime = $("#startTime").val().trim();
        var endTime = $("#endTime").val().trim();
        //var filed1 = $("#filed1").val();
        //var filed4 = $("#filed4").val();

        if (tel != "") {
            sel.addParams("tel", tel);
        } else {
            sel.addParams("tel", undefined);
        }
        if (province != "请选择省份") {
            sel.addParams("province", province);
        } else {
            sel.addParams("province", undefined);
        }
        if (city != "请选择城市") {
            sel.addParams("city", city);
        } else {
            sel.addParams("city", undefined);
        }
        if (hospitalName != "") {
            sel.addParams("hospitalName", hospitalName);
        } else {
            sel.addParams("hospitalName", undefined);
        }
        if (hospitalOffices != "") {
            sel.addParams("hospitalOffices", hospitalOffices);
        } else {
            sel.addParams("hospitalOffices", undefined);
        }
        if (startTime != "") {
            sel.addParams("startTime", startTime);
        } else {
            sel.addParams("startTime", undefined);
        }
        if (endTime != "") {
            sel.addParams("endTime", endTime);
        } else {
            sel.addParams("endTime", undefined);
        }
        //if(filed1 != ""){
        //    sel.addParams("filed1",filed1);
        //}else{
        //    sel.addParams("filed1",undefined);
        //}if(filed4 != ""){
        //    sel.addParams("filed4",filed4);
        //}else{
        //    sel.addParams("filed4",undefined);
        //}
        getData();
    })

    //导出
    $("#importBtn").on("click", function () {
        var data = "";
        var tel = $("#tel").val().trim();
        var province = $("#province").find("option:selected").text();
        var city = $("#city").find("option:selected").text();
        var hospitalName = $("#hospitalName").val().trim();
        var hospitalOffices = $("#hospitalOffices").val().trim();
        var startTime = $("#startTime").val().trim();
        var endTime = $("#endTime").val().trim();
        //var filed1 = $("#filed1").val().trim();
        //var filed4 = $("#filed4").val().trim();

        if (tel != "") {
            data += "&tel=" + tel;
            //sel.addParams("tel",tel);
        }
        if (province != "请选择省份") {
            data += "&province=" + province;
            // sel.addParams("province",province);
        }
        if (city != "请选择城市") {
            data += "&city=" + city;
            // sel.addParams("city",city);
        }
        if (hospitalName != "") {
            data += "&hospitalName=" + hospitalName;
            // sel.addParams("hospitalName",hospitalName);
        }
        if (hospitalOffices != "") {
            data += "&hospitalOffices=" + hospitalOffices;
            // sel.addParams("hospitalOffices",hospitalOffices);
        }
        if (startTime != "") {
            data += "&startTime=" + startTime;
            // sel.addParams("startTime",startTime);
        }
        if (endTime != "") {
            data += "&endTime=" + endTime;
            //  sel.addParams("endTime",endTime);
        }
        //if(filed1 != ""){
        //    data+="&filed1="+filed1;
        //  //  sel.addParams("filed1",filed1);
        //}
        //if(filed4 != ""){
        //    data+="&filed4="+filed4;
        //    //sel.addParams("filed4",filed4);
        //}
        window.location.href = getIP() + "member/getmemberExcel?type=0" + data;
    })

    //判断checkbox状态
    $("#selAll").on("click", function () {
        if (!$(this).attr("checked")) {
            $("input[type=checkbox]").attr("checked", "checked")
        } else {
            $("input[type=checkbox]").removeAttr("checked");
        }
    })



    //删除事件
    /*  $('#delBtn').on("click",function(){
          var params = getCheckData();
          if(params != "") {
              if(confirm("确定要删除吗！")){
                  var del = $.Params.create();
                  del.addParams("params", params)
                  del.ajax(getIP() + "metSort/deleteMetSort", function (data) {
                      if (data.success) {
                          alert("删除成功！");
                          getData();
                      }
                  })
              }
          }else{
              alert("请至少选中一项");
          }
      });
      */

    //获取数据
    function getData() {
        getPageData(sel, getIP() + "member/getmemberPage", function (data) {
            if (data.success) {
                var html = "";
                if (data.total > 0) {
                    $.each(data.result, function (i, val) {
                        var filed8 = "";
                        if (val.memberRespresent == null) {
                            filed8 = "未绑定";
                        } else {
                            filed8 = val.memberRespresent.name;
                        }
                        if (val.filed10 == null || val.filed10 == "") {
                            val.filed10 = "0";
                        }
                        if (val.province == null) {
                            val.province = "";
                        }
                        if (val.city == null) {
                            val.city = "";
                        }
                        if (val.hospitalName == null) {
                            val.hospitalName = "";
                        }
                        html += "<tr role='row'' class='even'>" +
                            "<td><input eid='" + val.id + "' onclick='getCheck(this)' type='checkbox' class='classOption'></td>" +
                            "<td class='sorting_1'>" + val.name + "</td>" +
                            "<td>" + val.tel + "</td>" +
                            "<td>" + val.province + "/" + val.city + "</td>" +
                            "<td>" + val.hospitalName + "</td>" +
                            "<td>" + val.hospitalOffices + "</td>" +
                            "<td><a data-reveal-id='selOthers' style='cursor:pointer' onclick='bindResPresent(\"" + val.id + "\",this)'>" + filed8 + "</a></td>" +
                            "<td>" + val.filed10 + "</td>" +
                            "<td>" + val.createTime + "</td>" +
                            "<td><button type='button' class='btn btn-primary'  onclick='updateInfo(\"" + val.id + "\")'>修改</button>" +
                            //"<button type='button' class='btn btn-primary' style='margin-left: 2px;'  onclick='updateOther(\"" + val.id + "\")'>设置为其他</button>"+
                            "</td>" +
                            "</tr>";
                    });
                } else {
                    html = "<tr style = 'text-align:center;'><td>暂无数据</td></tr>";
                }

                $("#listContainer").html(html);
            }
        })
    }

});
function updateInfo(eid) {
    window.location.href = "memberUpdate.html?eid=" + eid;
}

function updateOther(eid) {
    var selUpdate = $.Params.create();
    selUpdate.addParams("id", eid);
    selUpdate.addParams("type", 1);
    selUpdate.ajax(getIP() + "member/updmember", function (data) {
        if (data) {
            window.location.href = "memberList.html";
        }
    })
}

function bindResPresent(memberId, obj) {
    // e.preventDefault();
    var modalLocation = $(obj).attr('data-reveal-id');
    $(".reveal-modal-bg").css("visibility", "visible");
    $('#' + modalLocation).show();
    $('#' + modalLocation).reveal($(obj).data());

    var selRespresent = $.Params.create();
    getRespresentData();

    $("#doctor_selDataBtn").on("click", function () {
        var pTitle = $("#selDoctorVal").val();
        if (pTitle != "") {
            selRespresent.addParams("pTitle", pTitle);
        } else {
            selRespresent.addParams("pTitle", undefined);
        }
        getRespresentData();

    })

    function getRespresentData() {
        selRespresent.addParams("page", 0);
        selRespresent.addParams("rows", rowsExp);
        getPageDataExp(selRespresent, getIP() + "memberRespresent/getMemberRespresentPage", function (data) {
            if (data.success) {
                var respresentHtml = "";
                if (data.total > 0) {
                    $.each(data.result, function (i, val) {
                        var depart = "";
                        if (val.Department != null) {
                            depart = val.Department.name;
                        }
                        respresentHtml += "<tr role='row'' class='even'>" +
                            "<td class='sorting_1'>" + val.name + "</td>" +
                            "<td>" + val.tel + "</td>" +
                            "<td>" + depart + "</td>" +
                            "<td><button type='button' class='btn btn-primary'  onclick='bindPresent(\"" + val.userId + "\",\"" + memberId + "\")'>绑定</button></td></tr>";
                    })
                } else {
                    respresentHtml = "<tr role='row' class='even'><td colspan='4' class='txCenter'>暂无数据</td></tr>";
                }
                $("#othersListContainer").html(respresentHtml);
            }
        })
    }
}

function bindPresent(userId, memberId) {
    var memBindSel = $.Params.create();
    memBindSel.addParams("id", memberId);
    memBindSel.addParams("filed8", userId);
    memBindSel.ajax(getIP() + "member/updmember", function (data) {
        if (data.success) {
            alert("绑定成功！");
            $("#selOthers").css("display", "none");
            $(".reveal-modal-bg").css("display", "none");
            window.location.href = "memberList.html";
        }
    })
}
